package com.novax.ex.activity.provider.service;

import com.novax.ex.activity.infrastructure.entity.IeoConfig;
import com.novax.ex.activity.infrastructure.entity.IeoStages;
import com.novax.ex.activity.infrastructure.mapper.IeoConfigMapper;
import com.novax.ex.activity.infrastructure.mapper.IeoStagesMapper;
import com.novax.ex.common.base.BaseMapper;
import com.novax.ex.common.base.BaseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import jakarta.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Description ieo分期service
 * @Author ChenXi
 * @Date 2023/05/06/11:20
 */


@Service
public class IeoStagesService extends BaseService {

    @Resource
    private IeoStagesMapper ieoStagesMapper;

    @Resource
    private IeoConfigMapper ieoConfigMapper;

    @Override
    protected BaseMapper getBaseMapper() {
        return ieoStagesMapper;
    }

    /**
     * @Description: 获取ieo now之前的所有分期配置
     * @Author: ChenXi
     * @Date:  2023/5/6 11:23
     * @param
     * @return: java.util.List<com.novax.ex.activity.infrastructure.entity.IeoStages>
     **/
    public List<IeoStages> findByIeoId(Long ieoId, Date now) {
        return ieoStagesMapper.findByIeoId(ieoId, now);
    }

    /**
     * @Description: 查询最后一期的id
     * @Author: ChenXi
     * @Date:  2023/5/8 09:54
     * @param ieoId
     * @return: java.lang.Long
     **/
    public Long findLastIdByIeoId(Long ieoId) {
        return ieoStagesMapper.findLastIdByIeoId(ieoId);
    }

    /**
     * @Description: 查询所有配置
     * @Author: ChenXi
     * @Date:  2023/5/11 15:04
     * @param ieoId
     * @return: java.util.List<com.novax.ex.activity.infrastructure.entity.IeoStages>
     **/
    public List<IeoStages> findListByIeoId(Long ieoId) {
        return ieoStagesMapper.findListByIeoId(ieoId);
    }


    /**
     * @Description: 删除所有分期配置
     * @Author: ChenXi
     * @Date:  2023/5/11 15:40
     * @param ieoId
     * @return: void
     **/
    @Transactional(rollbackFor = RuntimeException.class)
    public boolean deleteByIeoId(Long ieoId, IeoConfig ieoConfig) {
        ieoStagesMapper.deleteByIeoId(ieoId);
        return ieoConfigMapper.updateByPrimaryKeySelective(ieoConfig) >= 1;
    }

    @Transactional(rollbackFor = RuntimeException.class)
    public boolean addStagesConfig(Long ieoId, List<IeoStages> ieoStagesList, IeoConfig ieoConfig) {
        // 删除所有配置
        ieoStagesMapper.deleteByIeoId(ieoId);
        // 添加配置
        return ieoStagesMapper.batchInsert(ieoStagesList) >= 1 && ieoConfigMapper.updateByPrimaryKeySelective(ieoConfig) >= 1;
    }

}
